<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

</body>
<script>
    var str1 = "aabccd";
    var obj = {};
    for (var i = 0; i < str1.length; i++) {
        var x = str1.charAt(i);
        if (obj[x] == undefined) {
            obj[x] = 1;
        } else {
            obj[x]++;
        }
    }
    console.log(obj);//出现次数
    var str2 = "";
    for (var i in obj) {
        str2 += i;
    }
    console.log(str2);//abcd
</script>

</html>